package org.weso.lodin.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.weso.lodin.appserv.DatasetAppServ;
import org.weso.lodin.appserv.TransformerAppServ;
import org.weso.lodin.dao.CountryDAO;
import org.weso.lodin.dao.CountryDAOImpl;
import org.weso.lodin.dao.DataInformationIndicatorDAOImpl;
import org.weso.lodin.dao.IndicatorDAO;
import org.weso.lodin.dao.ObservationDAO;
import org.weso.lodin.dao.ObservationDAOImpl;
import org.weso.lodin.dao.PrimaryObservationDAOImpl;
import org.weso.lodin.datasource.config.DatasetDirTemplateDAOImpl;
import org.weso.lodin.datasource.config.DatasetTemplateDAO;
import org.weso.lodin.datasource.config.DatasetTemplateDAOPrimaryImpl;
import org.weso.lodin.to.DatasetTO;
import org.weso.lodin.to.ObservationTO;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFWriter;
import com.hp.hpl.jena.vocabulary.DCTerms;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.hp.hpl.jena.vocabulary.XSD;

public class TransformerHelper {

	public static TransformerAppServ getPrimaryTransformer(String dir) {
		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
		InputStream inCountries = contextClassLoader.getResourceAsStream(
				TransformerAppServ.COUNTRIES_COUNTRIES_FAO_XLS);
		CountryDAO daoCountries = new CountryDAOImpl(inCountries);
		//Get Primaryindicators info
		DatasetTemplateDAO dao = new DatasetTemplateDAOPrimaryImpl(
				contextClassLoader.getResourceAsStream(TransformerAppServ.WEBINDEX_RAW_DATA_2_XLS));
		//Get Datasets metainformation
		Map<String, DatasetTO> datasetsInfo = new HashMap<String,DatasetTO>();	
		IndicatorDAO indicators = new DataInformationIndicatorDAOImpl(
				contextClassLoader.getResourceAsStream(TransformerAppServ.WEBINDEX_RAW_DATA_XLS));
		for(DatasetTO dataset:indicators.getPrimaryIndicators()){
			datasetsInfo.put(dataset.getCode(),dataset);	
			}		
		//Get observations of primaryindicators		
		ObservationDAO<ObservationTO> daoObservations = 
			new PrimaryObservationDAOImpl(
					contextClassLoader.getResourceAsStream(TransformerAppServ.WEBINDEX_RAW_DATA_2_XLS));
		//Get Transformer
		TransformerAppServ transformer = new TransformerAppServ(dao, daoCountries, daoObservations, datasetsInfo);
		return transformer;
	}
	

	public static TransformerAppServ getSecondaryTransformer(String dir) {
		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
		InputStream inCountries = contextClassLoader.getResourceAsStream(
				TransformerAppServ.COUNTRIES_COUNTRIES_FAO_XLS);
		CountryDAO daoCountries = new CountryDAOImpl(inCountries);
		InputStream inData = contextClassLoader.getResourceAsStream(
				TransformerAppServ.WEBINDEX_RAW_DATA_2_XLS);
		ObservationDAO<ObservationTO> daoObservations = new ObservationDAOImpl(inData);
		DatasetTemplateDAO dao = new DatasetDirTemplateDAOImpl(dir);			
		Map<String, DatasetTO> datasetsInfo = DatasetAppServ.getDatasets(TransformerAppServ.WEBINDEX_RAW_DATA_XLS);
		TransformerAppServ transformer = new TransformerAppServ(dao, daoCountries, daoObservations, datasetsInfo);
		return transformer;
	}

	public static void serializeModel(Model result, ResourceBundle mapPrefixesURIs, String file)
			throws FileNotFoundException, IOException {
		addPrefixes(result, mapPrefixesURIs);
		RDFWriter w = result.getWriter("TURTLE");	
		OutputStream os= new FileOutputStream(new File(file));
		w.write(result, os,TransformerAppServ.WEBINDEX_BASE_URI);	
		os.close();
	}
	
	public static void addPrefixes(Model result, ResourceBundle mapPrefixesURIs){
		Enumeration<String> prefixes = mapPrefixesURIs.getKeys();
		while(prefixes.hasMoreElements()){
			String prefix = prefixes.nextElement();
			result.setNsPrefix(prefix, mapPrefixesURIs.getString(prefix));
		}
		//Building (not extract)
		result.setNsPrefix("dcterms", DCTerms.getURI());
		result.setNsPrefix("xsd", XSD.getURI());
		result.setNsPrefix("owl", OWL.getURI());
		result.setNsPrefix("rdf", RDF.getURI());
		result.setNsPrefix("rdfs", RDFS.getURI());
	}
}
